package android.slc.icr.core.room.manager;

import android.slc.icr.core.constant.IcrConstants;
import android.slc.icr.core.entity.IcrChartRoom;
import android.slc.icr.core.entity.IcrMessage;
import android.slc.icr.core.room.studio.LiveChartRoomStudio;
import android.slc.icr.core.utils.IcrMessageUtils;

/**
 * 直播聊天室管理器
 *
 * @author slc
 * @date 2022/05/10 下午 1:00
 */
public class LiveChartRoomManager extends AbsChartRoomManager<LiveChartRoomStudio> {

    private static class Holder {
        private static final LiveChartRoomManager LIVE_CHART_ROOM_MANAGER = new LiveChartRoomManager();
    }

    public static LiveChartRoomManager getInstance() {
        return Holder.LIVE_CHART_ROOM_MANAGER;
    }

    @Override
    public String getRoomMsgType() {
        return IcrMessage.MSG_TYPE_CHART_ROOM_LIVE;
    }

    @Override
    public void onMessage(IcrMessage message) {
        String action = message.getAction();
        String sendId = message.getSender();
        String roomId = message.getRelateId();
        if (!IcrMessageUtils.checkIcrMessage(message)) {
            return;
        }
        switch (action) {
            case IcrConstants.Action.ACTION_CHART_ROOM_ACTOR_START:
            case IcrConstants.Action.ACTION_CHART_ROOM_ACTOR_STOP:
            case IcrConstants.Action.ACTION_CHART_ROOM_ACTOR_JOIN:
            case IcrConstants.Action.ACTION_CHART_ROOM_ACTOR_LEFT:
            case IcrConstants.Action.ACTION_CHART_ROOM_ACTOR_APPLY:
            case IcrConstants.Action.ACTION_CHART_ROOM_ACTOR_APPLY_HANDLER:
                LiveChartRoomStudio actorChartRoomStudio = chartRoomByRoomIdMap.get(roomId);
                if (actorChartRoomStudio == null) {
                    IcrMessageUtils.sendChartRoomIllicit(getIcrMessageRemoteService(), action,
                            IcrMessageUtils.STR_DOES_NOT_EXIST, createMustInfo(getRoomMsgType(), sendId, IcrChartRoom.builder().setRoomId(roomId).build()));
                    break;
                }
                actorChartRoomStudio.onOperateEvent(message);
                break;
            default:
                super.onMessage(message);
                break;
        }
    }

    @Override
    protected LiveChartRoomStudio createChartRoomStudio(IcrChartRoom icrChartRoom) {
        return new LiveChartRoomStudio(icrChartRoom, getIcrMessageRemoteService(), getIcrPersistenceService().getIcrChartRoomMemberService(), this);
    }
}
